package com.ruoyi.douyin.monitor;

import com.ruoyi.douyin.service.VideoService;
import com.ruoyi.system.domain.DouyinUser;
import com.ruoyi.system.domain.vo.DouyinVideoListVo;
import com.ruoyi.system.domain.DouyinVideo;
import com.ruoyi.system.service.DouyinUserService;
import com.ruoyi.system.service.DouyinVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class TimingVideoGet {
    @Autowired
    private DouyinVideoService douyinVideoService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private DouyinUserService douyinUserService;

    /**
     * 定时刷新用户的视频账户
     */
    //@Scheduled(cron = "0 0/5 * * * *")
    @Scheduled(cron = "0 0 1 * * ?")
    public void timingVideo() {
        System.out.println("开始批量更新数据");
        List<DouyinUser> douyinUsers = douyinUserService.getAllUser();
        List<DouyinVideoListVo> douyinVideoListVos = douyinUsers.stream().map(item -> {
            DouyinVideoListVo douyinVideoListVo = new DouyinVideoListVo();
            douyinVideoListVo
                    .setOpenId(item.getOpenId())
                    .setUserId(item.getUserId().toString())
                    .setAvatar(item.getAvatar())
                    .setNickname(item.getNickname())
                    .setCursor("0");
            return douyinVideoListVo;
        }).collect(Collectors.toList());
        if (douyinVideoListVos.size() == 0) return;
        List<DouyinVideo> douyinVideos = videoService.douyinVideoList(douyinVideoListVos, new ArrayList<>());
        //douyinVideoService.batchAddVideo(douyinVideos);
    }
}
